package com.neolix.watch;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.neolix.watch.util.FontTypefaceUtil;

import java.util.Calendar;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;

import static com.neolix.watch.NeolixApplication.getInstance;

public class TestActivity extends Activity {
    private ViewGroup mContainer;
    private ImageView myFrontIv;
    private ImageView myBackIv;
    private WheelView day;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mContainer = (ViewGroup) this.findViewById(R.id.mContainer);
        myFrontIv = (ImageView) this.findViewById(R.id.myFrontIv);
        myBackIv = (ImageView) this.findViewById(R.id.myBackIv);
        // 因为要旋转所以我们需要保存视图的缓存信息
        mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);

        myFrontIv.setImageBitmap(createReflectedImage(((BitmapDrawable) getResources()
                .getDrawable(R.drawable.j6_phone_watch)).getBitmap()));
        myBackIv.setImageBitmap(createReflectedImage(((BitmapDrawable) getResources()
                .getDrawable(R.drawable.j6_phone_weather)).getBitmap()));

        day = (WheelView) findViewById(R.id.day);
        day.setRemarkText("日");
        day.setRemarkFontSize(30);
        day.setRemarkTextColor(getResources().getColor(R.color.text_wheel_remark));

        day.setWheelBackground(R.drawable.bg_wheel_backgrond);
        NumericWheelAdapter minAdapter = new NumericWheelAdapter(getInstance(), 0, 59);
        minAdapter.setItemResource(R.layout.wheel_text_item);
        minAdapter.setItemTextResource(R.id.text);
        minAdapter.setTextColor(getResources().getColor(R.color.text_color_yellow));
        minAdapter.setFontType(FontTypefaceUtil.getTypefaceOfHelvetica());
        day.setViewAdapter(minAdapter);
        day.setCyclic(true);
    }

    public void showBack(View view) {
        applyRotation(1, 0, 90);
    }

    public void showFront(View view) {
        applyRotation(-1, 0, -90);
    }

    private void applyRotation(int position, float start, float end) {
        final float centerX = mContainer.getWidth() / 2.0f;
        final float centerY = mContainer.getHeight() / 2.0f;

        final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
                centerX, centerY, 310.0f, true);
        rotation.setDuration(500);
        rotation.setFillAfter(true);
        // 动画插入器 加速
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new DisplayNextView(position));

        mContainer.startAnimation(rotation);
    }

    private final class DisplayNextView implements Animation.AnimationListener {
        private final int mPosition;

        private DisplayNextView(int position) {
            mPosition = position;
        }

        @Override
        public void onAnimationStart(Animation arg0) {
        }

        @Override
        public void onAnimationEnd(Animation arg0) {
            mContainer.post(new SwapViews(mPosition));
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
        }
    }

    private final class SwapViews implements Runnable {
        private final int mPosition;

        public SwapViews(int position) {
            mPosition = position;
        }

        @Override
        public void run() {
            final float centerX = mContainer.getWidth() / 2.0f;
            final float centerY = mContainer.getHeight() / 2.0f;
            Rotate3dAnimation rotation = null;
            if (mPosition > -1) {
                myFrontIv.setVisibility(View.GONE);
                myBackIv.setVisibility(View.VISIBLE);
                myBackIv.requestFocus();

                rotation = new Rotate3dAnimation(-90, 0, centerX, centerY,
                        310.0f, false);
            } else {
                myFrontIv.setVisibility(View.VISIBLE);
                myBackIv.setVisibility(View.GONE);
                myFrontIv.requestFocus();

                rotation = new Rotate3dAnimation(90, 0, centerX, centerY,
                        310.0f, false);
            }

            rotation.setDuration(500);
            rotation.setFillAfter(true);
            // 动画插入器 减速
            rotation.setInterpolator(new DecelerateInterpolator());

            mContainer.startAnimation(rotation);
        }
    }

    private Bitmap createReflectedImage(Bitmap originalBitmap) {
        final int reflectionGap = 4;

        int width = originalBitmap.getWidth();
        int height = originalBitmap.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0,
                height / 2, width, height / 2, matrix, false);
        Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height
                + height / 2 + reflectionGap), Config.ARGB_8888);

        Canvas canvas = new Canvas(withReflectionBitmap);
        canvas.drawBitmap(originalBitmap, 0, 0, null);

        Paint defaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

        canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);

        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, originalBitmap.getHeight(), 0,
                withReflectionBitmap.getHeight(), 0x70ffffff, 0x00ffffff,
                TileMode.MIRROR);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

        canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint);

        return withReflectionBitmap;
    }
}